package com.zhixiang.core.util;

import java.text.SimpleDateFormat;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;
import net.sf.json.JsonConfig;
import net.sf.json.processors.JsonValueProcessor;
import net.sf.json.util.CycleDetectionStrategy;

public class JsonUtil
{

  public static String object2Json(Object object)
  {
      JsonConfig jsonConfig = new JsonConfig();
      jsonConfig.registerJsonValueProcessor(java.util.Date.class,
              new JsonValueProcessor() {
                  private final String format = "yyyy-MM-dd hh:mm:ss";
   
                  public Object processArrayValue(Object object,
                          JsonConfig jsonConfig) {
                      return null;
                  }
   
                  public Object processObjectValue(String string,
                          Object object, JsonConfig jsonConfig) {
                      if (null == object) {
                          return "";
                      } else {
                          if (object instanceof java.util.Date) {
                              SimpleDateFormat simpleDateFormat = new
                                  SimpleDateFormat(format);
                              String dateStr = simpleDateFormat
                                      .format(object);
                              return dateStr;
                          }
                      }
   
                      return object.toString();
                  }
              });
      jsonConfig.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);//解决转json的死循环问题策略
      if (object instanceof String){
          return object.toString().replaceAll("\"", "'");//双引号改单引号by GT
      }
      if ((object instanceof Object[]) || (object instanceof List)) {
          JSONArray jsonObject = JSONArray.fromObject(object, jsonConfig);
          //return jsonObject.toString() + '\n';
          return jsonObject.toString().replaceAll("\"", "'");
      } else {
          JSONObject jsonObject = JSONObject.fromObject(object, jsonConfig);
          //return jsonObject.toString() + '\n';
          return jsonObject.toString().replaceAll("\"", "'");
      }
  }

    @SuppressWarnings("deprecation")
    public static List<?> json2List(String json, Class<?> clazz)
    {
        JSONArray jsonArray = JSONArray.fromObject(json);
        List<?> output = JSONArray.toList(jsonArray, clazz);
        
//        JSONArray jsonArray1 = (JSONArray)JSONSerializer.toJSON(json);
//        List<?> output = (List<?>)JSONSerializer.toJava(jsonArray1);
        
        return output;
    }
    
    public static void main(String[] args)
    {
        String json = "[{'id':null,'paraName':'22','paraValue':'22','paraUpRange':0,'paraDownRange':0,'paraRate':100,'remark':'222'},{'id':null,'paraName':'33','paraValue':'33','paraUpRange':-2,'paraDownRange':5,'paraRate':100,'remark':'5333'}]";
//        JSONArray jsonArray = JSONArray.fromObject(json);
//        List<ProcessParameter> output = JSONArray.toList(jsonArray, ProcessParameter.class);
        
        JSONArray jsonArray1 = (JSONArray)JSONSerializer.toJSON(json);

        List<?> output = (List<?>)JSONSerializer.toJava(jsonArray1);
        
        System.out.println(output);
    }

}

